import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    component: () => import('../views/Login'),
    meta: {
      title: '登录'
    }
  },
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/index',
    component: () => import('../views/Index'),
    children: [
      {
        path: 'users',
        component: () => import('../views/Users')
      },
      {
        path: 'roles',
        component: () => import('../views/Roles')
      },
      {
        path: 'menus',
        component: () => import('../views/Menus')
      },
      {
        path: 'tags',
        component: () => import('../views/Tags')
      },
      {
        path: 'articles',
        component: () => import('../views/Articles')
      },
      {
        path: 'images',
        component: () => import('../views/Images')
      },
      {
        path: 'imageType',
        component: () => import('../views/ImageType')
      },
      {
        path: 'musics',
        component: () => import('../views/Musics')
      },
      {
        path: 'comment',
        component: () => import('../views/Comments')
      },
      {
        path: 'leaveComment',
        component: () => import('../views/LeaveComments')
      },
      {
        path: 'friendLink',
        component: () => import('../views/FriendLink')
      },
      {
        path: 'wallpaper',
        component: () => import('../views/WallpaperLib')
      },
      {
        path: 'setting',
        component: () => import('../views/SystemSetting')
      },
      {
        path: 'column',
        component: () => import('../views/Column')
      }
    ],
    meta: {
      title: '后台管理'
    }
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  if (to.meta.title) document.title = to.meta.title;
  if (to.path === '/login') return next()
  if (!sessionStorage.getItem('token')) return next('/login')
  next()
})

export default router
